##
#
# Snaarpy: Guitar chord parser and formatter
# Copyright (C) 2009 Alberto Gonzalez
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##

'''
Created on 03/05/2009

@author: albgonz
'''

import sys

CHORD_POS = {
             "A" : "x02220",
             "Am" : "x02210",
             "B"  : "x24442",
             "Bm" : "x24432",
             "B7" : "x21202",
             "Bb" : "x13331",
             "C" : "x32010",
             "Cmaj7" : "x32000",
             "D" : "xx0232",
             "D7": "xx0212",
             "E" : "022100",
             "Em" : "022000",
             "F" : "133211",
             "Fm" : "133111",
             "F#" : "244322",
             "G" :  "320003",
             "G7" : "320001"
             }

def chord_frets(chord):
    if chord in CHORD_POS:
        return CHORD_POS[chord]
    else:
        return None

def find_base_fret(chord):
    min = sys.maxint
    max = 0
    for string in chord:
        if string == 'x' or string == '0':
            pass
        else:
            n = int(string)
            if n > max:
                max = n
            if n < min:
                min = n
    assert max - min <= 4
    if min < 4:
        return 0
    else:
        return min - 1

def correct_base_fret(chord, base):
    res = ""
    for string in chord:
        if string == 'x' or string == '0':
            res = res + string
        else:
            res = res + str(int(string) - base)
    return res